package com.mairuide._frame.utils.excellistener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.mairuide.bill.DealFlow;
import com.mairuide.bill.DealFlowService;
import com.mairuide.bill.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by 敲代码的卡卡罗特
 * on 2019/1/8 11:08.
 * 资金管控平台的处理类  针对sheet2 交易明细表
 */
public class ZJGKPTExcelListener extends AnalysisEventListener {
    DealFlowService dealFlowService;
    private  List<DealFlow> list = new ArrayList<DealFlow>();
    private List<DealFlow> data = new ArrayList<DealFlow>();
    private String name="";
    private boolean flag=false;
    public ZJGKPTExcelListener(DealFlowService dealFlowService) {
        this.dealFlowService = dealFlowService;
    }

    @Override
    public void invoke(Object object, AnalysisContext context) {
        String pattern = "\\(.+\\).+";

        List<String> s = (List<String>)object;
        String content = s.get(0);
       /* if (!flag&&"附件2-交易明细表".equals(content)){
            flag=true;
        }else{
            throw new RuntimeException("不是资金查控平台表");
        }
        if (!flag) return;*/
        if(content==null) return;
        if(content.equals("序号")) return;
        boolean isMatch = Pattern.matches(pattern, content);
        if (isMatch){
            name = content.split("\\)")[1];
            return;
        }

        if (StringUtils.isBlank(name)) return;
        DealFlow dealFlow = new DealFlow();
        String TRADE_DATE = s.get(1);//交易时间
        String timestampString = DateUtils.getTimestampString(TRADE_DATE);
        if (timestampString!=null){
            dealFlow.setTradeDate(Timestamp.valueOf(DateUtils.getFormatDateString(timestampString)));
        }
        String ACCOUNT_MODE = s.get(2);//交易方式
        dealFlow.setAccountMode(ACCOUNT_MODE);
        String ACCOUNT_BODY_NUMBER = s.get(3);//交易主体账号
        dealFlow.setAccountBodyNumber(ACCOUNT_BODY_NUMBER);
        String ACCOUNT_BODY_CARD = ACCOUNT_BODY_NUMBER;//交易主体卡号
        dealFlow.setAccountBodyCard(ACCOUNT_BODY_CARD);
        String ACCOUNT_BODY = name;//交易主体
        dealFlow.setAccountBody(ACCOUNT_BODY);
        String ID_NUMBER = s.get(5);//身份证号码
        dealFlow.setIdNumber(ID_NUMBER);
        String ACCOUNT_BODY_BANK =s.get(6);//交易主体归属行
        dealFlow.setAccountBodyBank(ACCOUNT_BODY_BANK);
        String ACCOUNT_RIVAL_NUMBER =s.get(7);//交易对手账号
        dealFlow.setAccountRivalNumber(ACCOUNT_RIVAL_NUMBER);
        String ACCOUNT_RIVAL_CARD =ACCOUNT_RIVAL_NUMBER;//交易对手卡号
        dealFlow.setAccountRivalCard(ACCOUNT_RIVAL_CARD);
        if (name.equals(s.get(8))){
            String ACCOUNT_RIVAL =s.get(4);//交易对手
            dealFlow.setAccountRival(ACCOUNT_RIVAL);
            dealFlow.setSign(0);//收付标志(0:进；1：出)
        }else{
            String ACCOUNT_RIVAL =s.get(8);//交易对手
            dealFlow.setAccountRival(ACCOUNT_RIVAL);
            dealFlow.setSign(1);//收付标志(0:进；1：出)
        }
        String ACCOUNT_RIVAL_BANK =s.get(10);//交易对手归属行
        dealFlow.setAccountRivalBank(ACCOUNT_RIVAL_BANK);
        String CURRENCY =s.get(11);//币种
        dealFlow.setCurrency(CURRENCY);
        String ACCOUNT_MONEY =s.get(12);//交易金额
        if (ACCOUNT_MONEY!=null){
            dealFlow.setAccountMoney(new BigDecimal(ACCOUNT_MONEY));
        }
        dealFlow.setSerialNumber(s.get(13));//交易流水号
        /*String ACCOUNT_ADDRESS =s.get(14);//交易地点
        dealFlow.setAccountAddress(ACCOUNT_ADDRESS);*/
        String REMARKS =s.get(16);//备注
        dealFlow.setRemarks(REMARKS);
        String IP_ADDRESS =s.get(17);//IP地址
        dealFlow.setIpAddress(IP_ADDRESS);

        if (data.size()>0){
            String same=TRADE_DATE+ACCOUNT_BODY_NUMBER+s.get(4)+ACCOUNT_RIVAL_NUMBER+s.get(8)+ACCOUNT_MONEY;
            DealFlow d = data.get(data.size() - 1);
            DateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
            String format = d.getTradeDate()==null?"":sdf.format(d.getTradeDate());
            String same2=format+d.getAccountBodyNumber()+d.getAccountBody()+d.getAccountRivalNumber()+d.getAccountRival()+d.getAccountMoney();
            if (same.equals(same2)) return;
        }
        data.add(dealFlow);
        if(data.size()>=100){
            //doSomething();
            list.addAll(data);
            data = new ArrayList<DealFlow>();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        list.addAll(data);
    }
    public void doSomething(){
        this.dealFlowService.batchSave(data);
    }

    public List<DealFlow> getList() {
        return list;
    }
}
